public member function
<atomic>

std::atomic::operator T

operator T() const volatile noexcept;operator T() const noexcept;
Access contained value
Returns the stored value by val.

This is a type-cast operator: evaluating an atomic object in an expression that expects a value of its contained type (T), calls this member function, accessing the contained value.

This operation is atomic and uses sequential consistency (memory_order_seq_cst). To retrieve the value with a different memory ordering, see atomic::load.

Parameters

none

Return value

The contained value.
T is atomic's template parameter (the type of the contained value).

Return value

val

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// atomic::operator=/operator T example:
#include <iostream>       // std::cout
#include <atomic>         // std::atomic
#include <thread>         // std::thread, std::this_thread::yield

std::atomic<int> foo = 0;
std::atomic<int> bar = 0;

void set_foo(int x) {
  foo = x;
}
void copy_foo_to_bar () {
  while (foo==0) std::this_thread::yield();
  bar = static_cast<int>(foo);
}
void print_bar() {
  while (bar==0) std::this_thread::yield();
  std::cout << "bar: " << bar << '\n';
}

int main ()
{
  std::thread first (print_bar);
  std::thread second (set_foo,10);
  std::thread third (copy_foo_to_bar);
  first.join();
  second.join();
  third.join();
  return 0;
}

Output:
bar: 10


Data races

No data races (atomic operation). The operation uses sequential consistency (memory_order_seq_cst).

Exception safety

No-throw guarantee: never throws exceptions.

See also